package com.qijian.maindata.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qijian.common.exception.ServiceException;
import com.qijian.common.utils.StringUtils;
import com.qijian.maindata.domain.EpmEqp;
import com.qijian.maindata.mapper.EpmEqpMapper;
import com.qijian.maindata.service.IEpmEqpService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备Service业务层处理
 *
 * @author qijian
 * @date 2025-03-25
 */
@Service
@RequiredArgsConstructor
public class EpmEqpServiceImpl extends ServiceImpl<EpmEqpMapper, EpmEqp> implements IEpmEqpService {

    private final EpmEqpMapper epmEqpMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean importList(List<EpmEqp> epmEqpList) {
        if(epmEqpList!=null && epmEqpList.size()>0){
            epmEqpList.forEach(x ->{
                if(StringUtils.isBlank(x.getEqpCode())){
                    throw new ServiceException("E01:设备编码必须维护");
                }
            });
            for (EpmEqp epmEqp : epmEqpList) {
                LambdaQueryWrapper<EpmEqp> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(EpmEqp::getEqpCode,epmEqp.getEqpCode());
                List<EpmEqp> eqpList = epmEqpMapper.selectList(wrapper);
                if(eqpList!=null && eqpList.size()>0){
                    epmEqp.setId(eqpList.get(0).getId());
                }
            }
            return this.saveOrUpdateBatch(epmEqpList);
        }else {
            throw new ServiceException("E01:导入数据为空");
        }
    }
}
